<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="店铺">
        <el-select v-model="dataForm.storeId" clearable @focus="getStore" placeholder="请选择店铺">
          <el-option
            v-for="item in storeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="渠道">
        <el-radio-group v-model="dataForm.chanel">
          <el-radio :label="1">携程</el-radio>
          <el-radio :label="2">美团</el-radio>
          <el-radio :label="3">其他</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="渠道" prop="chanel">
        <el-input v-model="dataForm.chanel" placeholder="渠道"></el-input>
      </el-form-item> -->
      <el-form-item label="公钥" prop="accessKey">
        <el-input v-model="dataForm.accessKey" placeholder="accessKey"></el-input>
      </el-form-item>
      <el-form-item label="分销商Id" prop="sid">
        <el-input v-model="dataForm.sid" placeholder="parentID"></el-input>
      </el-form-item>
      <el-form-item label="密钥" prop="secretKey">
        <el-input v-model="dataForm.secretKey" placeholder="secretKey"></el-input>
      </el-form-item>
      <el-form-item label="XC:accToken" prop="accessToken">
        <el-input v-model="dataForm.accessToken" placeholder="accessToken"></el-input>
      </el-form-item>
      <el-form-item label="XC:refToken" prop="refreshToken">
        <el-input v-model="dataForm.refreshToken" placeholder="refreshToken"></el-input>
      </el-form-item>
      <el-form-item label="XC:expiresIn" prop="expiresIn">
        <el-input v-model="dataForm.expiresIn" placeholder="expiresIn"></el-input>
      </el-form-item>
      <el-form-item label="XC:uniqueId" prop="uniqueId">
        <el-input v-model="dataForm.uniqueId" placeholder="uniqueId"></el-input>
      </el-form-item>
      <el-form-item label="预定渠道佣金比例" prop="commRate">
        <el-input v-model="dataForm.commRate" placeholder="佣金比例"></el-input>
      </el-form-item>
      <el-form-item label="飞猪佣金比例" prop="commRateFz">
        <el-input v-model="dataForm.commRateFz" placeholder="佣金比例"></el-input>
      </el-form-item>
      <el-form-item label="验价容忍" prop="priceTolerance">
        <el-input v-model="dataForm.priceTolerance" placeholder="验价容忍"></el-input>
      </el-form-item>
      <el-form-item label="直连验价百分比" prop="profitPercent">
        <el-input v-model="dataForm.profitPercent" placeholder="验价容忍"></el-input>
      </el-form-item>
      <el-form-item label="直连验价固定额" prop="profitPrice">
        <el-input v-model="dataForm.profitPrice" placeholder="验价容忍"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        storeId: "",
        chanel: "",
        accessKey: "",
        sid: "",
        secretKey: "",
        accessToken: "",
        refreshToken: "",
        expiresIn: "",
        uniqueId: "",
        commRate: "",
        commRateFz: "",
        priceTolerance: "",
        profitPercent: "",
        profitPrice: "",
      },
      storeList: [],
      dataRule: {
        storeId: [{ required: true, message: "分销商编码不能为空", trigger: "blur" }],
        chanel: [{ required: true, message: "渠道其他不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/dt/channel/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.storeId = data.dtOrderChannels.storeId;
              this.dataForm.chanel = data.dtOrderChannels.chanel;
              this.dataForm.accessKey = data.dtOrderChannels.accessKey;
              this.dataForm.sid = data.dtOrderChannels.sid;
              this.dataForm.secretKey = data.dtOrderChannels.secretKey;
              this.dataForm.accessToken = data.dtOrderChannels.accessToken;
              this.dataForm.refreshToken = data.dtOrderChannels.refreshToken;
              this.dataForm.expiresIn = data.dtOrderChannels.expiresIn;
              this.dataForm.uniqueId = data.dtOrderChannels.uniqueId;
              this.dataForm.commRate = data.dtOrderChannels.commRate;
              this.dataForm.commRateFz = data.dtOrderChannels.commRateFz;
              this.dataForm.priceTolerance = data.dtOrderChannels.priceTolerance;
              this.dataForm.profitPercent = data.dtOrderChannels.profitPercent;
              this.dataForm.profitPrice = data.dtOrderChannels.profitPrice;
            }
          });
        }
      });
    },
    // 查询店铺信息
    getStore() {
      this.$http({
        url: this.$http.adornUrl('/dt/store/storelist'),
        method: 'get',
        params: this.$http.adornParams({
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.storeList = data.list
          console.info(data.list)
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/dt/channel/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              storeId: this.dataForm.storeId,
              chanel: this.dataForm.chanel,
              accessKey: this.dataForm.accessKey,
              sid: this.dataForm.sid,
              secretKey: this.dataForm.secretKey,
              accessToken: this.dataForm.accessToken,
              refreshToken: this.dataForm.refreshToken,
              expiresIn: this.dataForm.expiresIn,
              uniqueId: this.dataForm.uniqueId,
              commRate: this.dataForm.commRate,
              commRateFz: this.dataForm.commRateFz,
              priceTolerance: this.dataForm.priceTolerance,
              profitPrice: this.dataForm.profitPrice,
              profitPercent: this.dataForm.profitPercent
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
